var fs=require("fs");
var gd=require("gd");
exports.add=[{
	_type:"functions",
	_section:"Functions",

	/**
	 * Function: Functions.send_sms_iqsms
	 */
	send_sms_iqsms: function(phone,text) {
		var url = "http://gate.iqsms.ru/send/?phone="+encodeURIComponent(phone)+"&text="+encodeURIComponent(text)+"&login="+this.site.iqsms.login+"&password="+this.site.iqsms.password+(this.site.iqsms.sender?"&sender="+this.site.iqsms.sender:"");
		var http=require("http");
		var request=new http.ClientRequest(url);
		var response = request.send(true);
		if (this.site.iqsms.debug) {
			var f=new fs.File(this.site.paths.basepath+"logs/sms.log");
			f.open("a");
			f.write(""+(new Date())+"\nSMS to "+phone+"\nresult ("+response.status+"):\n");
			f.write(response.data);
			f.write("\n");
			f.close();
		}
		return "";
	},

	/**
	 * Function: Functions.send_sms_db
	 */
	send_sms_db: function(phone,text) {
		var sms = this.site.models.SmsMessage.Create();

		sms.phone_to = phone;
		sms.body = text;
		sms.status_id = 1;
		sms.sender = this.site.iqsms.sender || null;

		sms.SaveAll();

		return sms;
	},
	
	/**
	 * Function: Functions.send_sms_from_db
	 */
	send_sms_from_db: function() {
		var t=this;
		var smses=this.site.sql.execute_and_fetch_single("select * from Tsms_messages where status_id=1");
		smses.forEach(function(sms) {
			t.site.sql.execute_single("update Tsms_messages set date_mo=now(),status_id=:status_id where id=:id",{status_id:3,id:sms.id});
		});
		smses.forEach(function(sms) {
			
			t.site.sql.execute_single("update Tsms_messages set date_mo=now(),status_id=:status_id where id=:id",{status_id:3,id:sms.id});
		});
	},

	/**
	 * Function: Functions.send_mail
	 *
	 */
	send_mail: function(code,hash,quiet,files) {
		if (!hash) hash={};
		var mail=this.site.models.MailTemplate.List("get_by_code",{code:code})[0];
		if (!mail) {
			if (quiet) return;
			throw new Error("No mail template "+code+" found in database");
		}
		if (!hash.from) hash.from=mail.default_from;
		if (!hash.to) {
			if (!mail.default_to) {
				if (quiet) return;
				throw new Error("No recipient and no default recipient given");
			}
			hash.to=mail.default_to.split(/\s*[;,]\s*/);
		}
		
		if (!hash.from) hash.from="no-reply@"+this.site.names[0];
		var headers = {
			From: hash.from,
			"Content-Type": 'text/plain; charset="UTF-8"'
		};
		if (hash.to.constructor!=Array) hash.to=[hash.to];
		this.mail=hash;
		var subject=this.views.apply(mail.subject,this);
		var body=this.views.apply(mail.body,this);
		var bodytext=body.replace(/<(\/h\d+|\/p|\/div|br|br\s*\/)\s*>\s*/gi,"\n").replace(/<.*?>/g,"");
		var rm=require("mail");
			

		headers["Content-Type"]='text/html; charset="UTF-8"';
		for (var i=0;i<hash.to.length;i++) {
			var arr=hash.to[i].match(/^\{\{(.*)\}\}$/);
			if (arr) eval("hash.to[i]=this."+arr[1]+";");
			//if (hash.to[i].match(/\w@\w/)) rm.mail(hash.to[i],subject,bodytext,headers,undefined,[{data:body,"Content-Type":"text/html; chartset=\"UTF-8\""}]);
			if (hash.to[i].match(/\w@\w/)) rm.mail(hash.to[i],subject,body,headers,undefined,files);
		}
	},

	/**
	 * Function: Functions.gen_page_numbers
	 *		Generates page numbers
	 * Parameters:
	 *		nowpage typeof Integer		- Current page, in 0 .. (pagescount-1)
	 *		pagescount typeof Integer	- Number of pages
	 * Returns:
	 *		typeof Array
	 *			item typeof Object		- { i: 0, page: i+1, active: 0 or 1 }
	 *			item typeof Object		- { split: 1 }
	 */
	gen_page_numbers:function(nowpage,pagescount) {
		var pages=[];
		function add_pages(start,end) {
			for (var i=start;i<end;i++) {
				pages.push({i:i,page:i+1,active: nowpage==i?1:0});
			}
		}
		if (pagescount<=10) {
			add_pages(0,pagescount);
		} else {
			if (nowpage<5) {
				add_pages(0,6);pages.push({split:1});add_pages(pagescount-1,pagescount);
			} else if (nowpage>=pagescount-5) {
				add_pages(0,1);pages.push({split:1});add_pages(pagescount-6,pagescount);
			} else {
				add_pages(0,1);pages.push({split:1});add_pages(nowpage-3,nowpage+4);pages.push({split:1});add_pages(pagescount-1,pagescount);
			}
		}
		return pages;
	},

	recursor: function(arr,func) {
		var level=1;
		while (arr.length) {
			var tmp=arr;
			arr=[];
			for (var i=0;i<tmp.length;i++) {
				var r=func(tmp[i],level);
				if (r) arr=arr.concat(r);
			}
			level++;
		}
	}

}];

